package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.entities.Health;
import com.goods.business.mapper.HealthMapper;
import com.goods.business.service.HealthService;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Decheng Kong
 * @date 2021/11/9 23:52
 */
@Service
public class HealthServiceImpl implements HealthService {
    @Autowired
    private HealthMapper healthMapper;

    @Override
    public Health findHealthyCheckInRecord(Long userId) {
        Example example = new Example(Health.class);
        example.createCriteria().andEqualTo("userId", userId).andCondition("date(create_time)=", LocalDate.now());
        return healthMapper.selectOneByExample(example);
    }

    @Override
    public void checkIn(HealthVO healthVO) {
        Health health = new Health();
        BeanUtils.copyProperties(healthVO, health);
        health.setCreateTime(new Date());
        healthMapper.insertSelective(health);
    }

    @Override
    public PageVO<HealthVO> findAllRecord(Integer pageNum, Integer pageSize, Long userId) {
        PageHelper.startPage(pageNum, pageSize);

        Example example = new Example(Health.class);
        example.orderBy("createTime").desc();
        example.createCriteria().andEqualTo("userId", userId);

        List<Health> health = healthMapper.selectByExample(example);
        List<HealthVO> collect = health.stream().map(health1 -> {
            HealthVO healthVO = new HealthVO();
            BeanUtils.copyProperties(health1, healthVO);
            return healthVO;
        }).collect(Collectors.toList());
        PageInfo<Health> info = new PageInfo<>(health);
        return new PageVO<>(info.getTotal(), collect);

    }

    /*

    <li>打卡人:{{userInfo.username}}</li>
     <li>打卡位置:{{item.address}}</li>
     <li>打卡时间:{{item.createTime}}</li>
     <li>健康状况:
         <div style="float: right">
               <el-tag v-if="item.situation===0" size="small" effect="dark" type="success">健康</el-tag>
        <el-tag v-else-if="item.situation===1" size="small" effect="dark" type="danger">发热</el-tag>
        <el-tag v-else-if="item.situation===2"  size="small" effect="dark"  type="warning">其他情况</el-tag>
         </div>
     </li>
     */
}
